package starfire.gui.defaultModelAndRenderer;

import java.io.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.*;

import starfire.ftp.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author zzzhc.starfire@gmail.com
 * @version 1.0
 */

public class FileListRenderer
    extends JLabel implements javax.swing.ListCellRenderer {
  static protected final Color SelectedBackgroundColor = Color.yellow;
  static FileSystemView fsv = FileSystemView.getFileSystemView();
  static Icon dirIcon = null;
  //static Icon fileIcon = null;
  boolean hasFocus = false;
  boolean selected = false;
  String name = null;
  Color textBackColor = null;

  public FileListRenderer() {
    this.setFont(new Font("Dialog", 0, 12));
    if (dirIcon==null)
    {
      try
      {
        File f = new File(".").getCanonicalFile();
        /*
        dirIcon = new ImageIcon(
            starfire.gui.defaultModelAndRenderer.FileListRenderer.class.
            getResource("dir.gif"));
          */

         dirIcon = fsv.getSystemIcon(f);
       /* fileIcon = new ImageIcon(
            starfire.gui.defaultModelAndRenderer.FileListRenderer.class.
            getResource("file.gif"));
          */
      }catch (Exception e){}
    }
  }

  public Component getListCellRendererComponent(JList list, Object value,
                                                int index, boolean isSelected,
                                                boolean cellHasFocus) {

    if (value instanceof File) {
      File f = (File) value;
      if (f instanceof FtpFile) {
        name = f.getName();
        if (f.isDirectory())
        {
          if (dirIcon!=null)
            this.setIcon(dirIcon);
        }
        else
        {
          setIcon(IconCollection.getIcon(f));
         // this.setIcon(fileIcon);
        }
      }
      else {
        name = fsv.getSystemDisplayName(f);
        setIcon(fsv.getSystemIcon(f));
      }
      setText(name);
      setToolTipText(name);
      selected = isSelected;
      if (isSelected) {
        //textBackColor = list.getSelectionBackground();
        //this.setBackground(list.getSelectionBackground());
      //  setBackground(SelectedBackgroundColor);
      }
      else
      {
        setBackground(Color.white);
      }
      return this;
    }
    else {
      return this;
    }
  }


   public void paint(Graphics g) {
     Color            bColor;
     Icon             currentI = getIcon();
     Font font = getFont();
     FontMetrics fm = g.getFontMetrics(font);
     int width = fm.stringWidth(this.getText());
     if(selected)
        bColor = SelectedBackgroundColor;
    else if(getParent() != null)
        bColor = getParent().getBackground();
    else
        bColor = getBackground();
     g.setColor(bColor);
     if (currentI != null && getText() != null) {
       int offset = (currentI.getIconWidth() + getIconTextGap());
       if (getComponentOrientation().isLeftToRight()) {
         g.fillRect(offset, 0, width,
                    getHeight() - 1);
       }
       else {
         g.fillRect(0, 0, width, getHeight() - 1);
       }
     }
     else
       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
     super.paint(g);
 }

}
